package cn.tedu.order.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tedu.order.entity.Order;
import cn.tedu.order.fegin.AccountClient;
import cn.tedu.order.fegin.EasyIdGeneratorClient;
import cn.tedu.order.fegin.StorageClient;
import cn.tedu.order.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;

@Service
public class OrderServiceImpl implements OrderService{
	
	@Autowired
    private OrderMapper orderMapper;
	 @Autowired
	 EasyIdGeneratorClient easyIdGeneratorClient;
	 @Autowired
	 private AccountClient accountClient;
	  @Autowired
	  private StorageClient storageClient;


	@Override
	  @GlobalTransactional
	public void create(Order order) {
		 Long orderId =easyIdGeneratorClient.nextId("order_business");
	      order.setId(orderId);
	      
	      if (Math.random() < 0.5) {
	            throw new RuntimeException("模拟异常");
	        }

	        orderMapper.create(order);
	        //storageClient.decrease(order.getProductId(), order.getCount());
	        //accountClient.decrease(order.getUserId(), order.getMoney());
		
	}

}
